package com.xu.controller.center;

import com.xu.controller.BaseController;
import com.xu.pojo.Orders;
import com.xu.pojo.vo.OrderStatusCountsVO;
import com.xu.service.center.MyOrderService;
import com.xu.utils.JSONResult;
import com.xu.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;

/**
 * @author anan
 * @create 2022-02-24 9:41
 */
@RestController
@RequestMapping("myorders")
@Api(value = "用户中心我的订单", tags = "用户中心我的订单相关接口")
public class MyOrderController extends BaseController {

    @Autowired
    private MyOrderService myOrderService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/query")
    public JSONResult query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false)
            @RequestParam Integer orderStatus,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize) {

        if(StringUtils.isBlank(userId)) return JSONResult.errorMsg(null);
        if(page == null){
            page = 1;
        }
        if(pageSize == null){
            pageSize = COMMEN_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = myOrderService.queryMyOrders(userId, orderStatus, page, pageSize);
        return JSONResult.ok(pagedGridResult);
    }

    // 商家发货没有后端，所以这个接口仅仅只是用于模拟
    @ApiOperation(value="商家发货", notes="商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public JSONResult deliver(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId){

        if (StringUtils.isBlank(orderId)) {
            return JSONResult.errorMsg("订单ID不能为空");
        }
        myOrderService.updateDeliverOrderStatus(orderId);
        return JSONResult.ok();
    }

    @ApiOperation(value="用户确认收货", notes="用户确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public JSONResult confirmReceive(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) throws Exception {
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }
        boolean res = myOrderService.updateReceiveOrderStatus(orderId);
        if(!res){
            return JSONResult.errorMsg("订单确认收货失败！");
        }

        return JSONResult.ok();
    }

    @ApiOperation(value="用户删除订单", notes="用户删除订单", httpMethod = "POST")
    @PostMapping("/delete")
    public JSONResult delete(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) throws Exception {
        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(checkResult.getStatus() != HttpStatus.OK.value()){
            return checkResult;
        }

        boolean res = myOrderService.deleteOrder(userId, orderId);
        if(!res){
            return JSONResult.errorMsg("订单删除失败");
        }
        return JSONResult.ok();
    }

    @ApiOperation(value="获得订单状态数概况", notes="获得订单状态数概况", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public JSONResult statusCounts(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId){

        if (StringUtils.isBlank(userId)) {
            return JSONResult.errorMsg("用户ID不能为空");
        }
        OrderStatusCountsVO result = myOrderService.getOrderStatusCounts(userId);

        return JSONResult.ok(result);
    }

    @ApiOperation(value="查询订单动向", notes="查询订单动向", httpMethod = "POST")
    @PostMapping("/trend")
    public JSONResult trend(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "查询分页每一页的显示条数", required = false)
            @RequestParam Integer pageSize){

        if (StringUtils.isBlank(userId)) {
            return JSONResult.errorMsg("用户ID不能为空");
        }
        PagedGridResult result = myOrderService.getOrderTrend(userId, page, pageSize);
        return JSONResult.ok(result);
    }






}
